package org.streamerstudio.ui.properties;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.databinding.EMFObservables;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.ColumnLayout;
import org.eclipse.ui.forms.widgets.ColumnLayoutData;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.streamerstudio.model.gstreamer.registry.GstObject;
import org.streamerstudio.model.gstreamer.registry.RegistryPackage.Literals;

public class DescriptionPropertyComposite extends Composite {

	private DataBindingContext m_bindingContext;
	private GstObject object;
	private FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	private Text text;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public DescriptionPropertyComposite(Composite parent, int style) {
		super(parent, style);

		createContents();
	}
	protected void createContents() {
		toolkit.adapt(this);
		toolkit.paintBordersFor(this);
		ColumnLayout columnLayout = new ColumnLayout();
		columnLayout.maxNumColumns = 1;
		setLayout(columnLayout);
		// -- component creation
		toolkit.createLabel(this, "Description", SWT.NONE);
		// -- component creation
		text = toolkit.createText(this, "New Text", SWT.WRAP | SWT.MULTI);
		ColumnLayoutData columnLayoutData = new ColumnLayoutData();
		columnLayoutData.heightHint = 100;
		text.setLayoutData(columnLayoutData);
		text.setText("");
		m_bindingContext = initDataBindings();
	}
	
	public void setInput(GstObject model){
		this.object = model;
		m_bindingContext = initDataBindings();
	}

	public void updateComposite(){
		m_bindingContext.updateTargets();
	}
	
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue textObserveTextObserveWidget = SWTObservables.observeText(text, SWT.NONE);
		IObservableValue objectDescriptionObserveValue = EMFObservables.observeValue(object, Literals.GST_OBJECT__DESCRIPTION);
		bindingContext.bindValue(textObserveTextObserveWidget, objectDescriptionObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST), new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
		//
		return bindingContext;
	}
}
